/**
 * Created by mar105 on 2017-11-14.
 */
import React, { Component } from 'react';
import * as config from '../../utils/config';

export default (ChildComponent) => {
  return class Base extends Component {
    constructor(props) {
      super(props);
      this.state = {
        sTabId: Object.keys(props.app.secondppopupPane !== undefined ? props.app.secondppopupPane : '').length > 0 ? props.app.secondppopupPane.key : (Object.keys(props.app.ppopupPane !== undefined ? props.app.ppopupPane : '').length > 0 ? props.app.ppopupPane.key : props.app.currentPane.key),
        formId: Object.keys(props.app.secondppopupPane !== undefined ? props.app.secondppopupPane : '').length > 0 ? props.app.secondppopupPane.formId : (Object.keys(props.app.ppopupPane !== undefined ? props.app.ppopupPane : '').length > 0 ? props.app.ppopupPane.formId : props.app.currentPane.formId), // 获取配置数据用的modelId
        formRoute: Object.keys(props.app.secondppopupPane !== undefined ? props.app.secondppopupPane : '').length > 0 ? props.app.secondppopupPane.route : (Object.keys(props.app.ppopupPane !== undefined ? props.app.ppopupPane : '').length > 0 ? props.app.ppopupPane.route : props.app.currentPane.route), // 组件名： 路由名称
        formData: [], // 获取配置数据
        gdsformconst: [], // 获取配置常量
        gdsjurisdiction: [], // 获取配置权限
        /* -----------------------------
                paramInfo: { dataId: '', srcTabId: '', srcComponentName: '' }
                dataId: 传递的数据id，
                srcTabId: 源单的tabid，后面用于数据更新同步刷新,
                srcComponentName:  源单的componentName，后面用于数据更新同步刷新
                ---------------------------- */
        paramInfo: props.app.ppopupPane === undefined || props.app.secondppopupPane === undefined ? props.app.currentPane.paramInfo : '', // 界面与另一个界面交互的信息
        pageLoading: true, // 整个页面loading
        billnosetting: {},
        report: [], /* 打印模板 */
      };
    }

    componentWillMount() {
      const url = `${config.server_host}business/getModelBysId/${this.state.formId}?sModelsId=${this.state.formId}`;
      const { sTabId, formRoute } = this.state;
      const { dispatch } = this.props;
      dispatch({
        type: 'content/getUrlData',
        payload: {
          urlDataType: 'First', url, sTabId, formRoute,
        },
      });
    }

    componentWillReceiveProps(nextProps) {
      const { dataList } = nextProps.content;
      const {
        sDataListTabId, formRoute, urlData, loading, returnType,
      } = dataList;
      if (this.state.sTabId === sDataListTabId
        && (this.state.sTabId === (Object.keys(nextProps.app.secondppopupPane !== undefined ? nextProps.app.secondppopupPane : '').length > 0 ? nextProps.app.secondppopupPane.key : (nextProps.app.ppopupPane !== undefined ? nextProps.app.ppopupPane.key : '')) || this.state.sTabId === nextProps.app.currentPane.key)
        && this.state.formRoute === formRoute &&
        this.state.formData.length <= 0 &&
        this.state.gdsformconst.length <= 0 && this.state.gdsjurisdiction.length <= 0) {
        if (loading) {
          this.setState({ loading: true });
          return;
        }
        if (returnType !== undefined && returnType === 'CUD') {
          return;
        }
        const {
          formData, gdsformconst, gdsjurisdiction, billnosetting, report,
        } = urlData;
        this.setState({
          formData, gdsformconst, gdsjurisdiction, billnosetting, report, pageLoading: false,
        });
      }
    }
    /* 上查下查通用方法 */
    searchUpDown =(sActiveId, menuConfig) => {
      console.log('config...', sActiveId, menuConfig);
      if (sActiveId !== undefined) {
        const { dispatch } = this.props;
        const sTabId = new Date().getTime().toString(); /* Tab标签Id */
        const url = `${config.server_host}gdsmodule/getGdsmoduleById/${sActiveId}?sModelsId=${sActiveId}`;
        dispatch({
          type: 'content/getUrlData',
          payload: {
            url,
            sTabId,
            sParentId: this.state.sTabId,
            urlDataType: 'newPane',
            // TODO: 上查下查根据从表数据sSrcId去动态展示下拉选项进行菜单展示
            checkedId: sActiveId,
            disabled: true,
          },
        });
      }
    }
    render() {
      return (<ChildComponent {...this.props} {...this.state} searchUpDown={this.searchUpDown} />);
    }
  };
};

